package fi.iki.joker.mobilogger;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import fi.iki.joker.mobilogger.db.DBUtil;
import fi.iki.joker.mobilogger.db.MemoBean;
import fi.iki.joker.mobilogger.util.AlertUtil;

/**
 * Lists the locally saved memos and allows user to edit and delete tham.
 * 
 * @author juhak
 *
 */
public class ListMemos extends Activity implements
		AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {

	private int currentlySelectedItem = -1;
	private DBUtil db = null;
	private String[] listData = null;
	private final String TAG = "ListBlogConfigs";
	private ListView list = null;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.managedata);
		TextView tv = (TextView) findViewById(R.id.selected_button_view);
		tv.setText("Please select a memo from the list");
		ButtonBar.createButtons(this);
		ButtonBar.requestFocusForButton(ButtonBar.BUTTON_MEMO);
		list = (ListView) findViewById(R.id.shown_data_list);
		updateListData(list);
		list.setOnItemSelectedListener(this);
		list.setOnItemClickListener(this);
	}
	
	private void updateListData(ListView list) {
		if (db == null) {
			db = new DBUtil();
		}
		MemoBean[] tempArr = db.getMemos(this);
		if(tempArr != null) {
			list.setVisibility(View.VISIBLE);
			listData = new String[tempArr.length];
			for (int i = 0; i < tempArr.length; i++) {
				MemoBean bc = tempArr[i];
				int id = bc.getId();
				if (bc == null) {
					listData[i] = "null";
				} else {
					listData[i] = new String(bc.toString());
				}
			}
			list.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, listData));
		} else {
			list.setVisibility(View.INVISIBLE);
		}
	}

	public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		this.currentlySelectedItem = arg2;
	}

	public void onNothingSelected(AdapterView arg0) {
		this.currentlySelectedItem = -1;
	}

	public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
		this.currentlySelectedItem = arg2;
		//This this though!
		AlertUtil.showAlert(this,"Select operation","What do you want to do?", "Edit",
				mEditListener, "Delete", mDeleteListener);
	}

	OnClickListener mEditListener = new OnClickListener() {
		public void onClick(DialogInterface df, int item) {
			editMemo(getMemoId(listData[currentlySelectedItem]));
		}
	};

	OnClickListener mDeleteListener = new OnClickListener() {
		public void onClick(DialogInterface df, int item) {
			deleteEntry(getMemoId(listData[currentlySelectedItem]));
			if(list != null) {
				updateListData(list);
			}
		}
	};

	private void editMemo(int memoId) {
		Intent intent = new Intent();
		intent.setClass(this, MemoEditor.class);
		intent.setAction(Intent.ACTION_EDIT);
		String URIStr = "content://fi.iki.joker.mobilogger/memo/"+memoId;
		intent.setData(Uri.parse(URIStr));
		this.startActivity(intent);
		this.finish();
	}

	private void deleteEntry(int entryId) {
		if (db != null) {
			db.deleteMemoItem(this,entryId);
		} else {
			Log.e(TAG, "Database not ready while deleting memo.");
		}
		
	}

	/**
	 * This is ugly, we get the id from the [num]-Title, should probably be done
	 * otherwise but this is simple
	 */

	private int getMemoId(String memoTitle) {
		if (memoTitle == null)
			return -1;
		int start = 1; // the [
		int end = memoTitle.indexOf(']');
		String number = memoTitle.substring(start, end);
		int res = -1;
		try {
			res = Integer.parseInt(number);
		} catch (NumberFormatException e) {
			Log.e(TAG, "Error parsing entry ID out of String: " + memoTitle
					+ " Start: " + start + " End: " + end);
			return -1;
		}
		return res;
	}
}
